package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //先判断是否存在这个  菜品或者套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();   //获取  当前用户的id
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart); //查询当前用户的购物车数据
        //如果存在，则数量加1
        if (shoppingCartList != null && shoppingCartList.size() > 0)  {
            ShoppingCart cart1 = shoppingCartList.get(0);
            cart1.setNumber(cart1.getNumber() + 1);
            //这个id是数据库中已有的 独一无二的  从list获取的
            shoppingCartMapper.updateNumberById(cart1);
        }else {
            //不存在，则添加
            //添加之前 需要判断是 套餐还是菜品
            if (shoppingCart.getDishId() != null) {
                //为菜品
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else if (shoppingCart.getSetmealId() != null) {
                //为套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.add(shoppingCart);
        }

    }

    @Override
    public List showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> showShoppingCart = shoppingCartMapper.list(shoppingCart);
        return showShoppingCart;
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setId(BaseContext.getCurrentId());
        //先判断删除的数量 是否大于 1
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart1 = shoppingCartList.get(0);
        if (cart1.getNumber() > 1) {
            //删除数量大于1
            cart1.setNumber(cart1.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart1);
        }else if (cart1.getNumber()  == 1){
            //删除数量等于1 直接删除
            shoppingCartMapper.delete(shoppingCart);
        }

    }

    @Override
    public void deleteAll() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }
}
